package customize

import (
	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/common/response"
	"gitcode.com/G-YT/baoyou/server/model/customize"
	"gitcode.com/G-YT/baoyou/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type MedicalHistoryApi struct {
}

// SaveMedicalHistory 保存病史
// @Tags disease
// @Summary 保存病史
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /history/saveDiseaseHistory [post]
func (m *MedicalHistoryApi) SaveMedicalHistory(c *gin.Context) {
	var du customize.UserMedicalHistory
	err := c.ShouldBindJSON(&du)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Current":   {utils.NotEmpty()},
		"CMeasure":  {utils.NotEmpty()},
		"CDrugInfo": {utils.NotEmpty()},
		"Past":      {utils.NotEmpty()},
		"PMeasure":  {utils.NotEmpty()},
		"PDrugInfo": {utils.NotEmpty()},
	}
	if err := utils.Verify(du, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	id := utils.GetUserID(c)
	du.ID = id
	if err := MedicalHistoryService.SaveMedicalHistory(&du); err != nil {
		global.GVA_LOG.Error("提交失败!", zap.Error(err))
		response.FailWithMessage("提交失败", c)
	} else {
		response.Result(200, "", "提交成功", c)
	}
}

// GetUserMedicalHistory 获取用户病史
// @Tags disease
// @Summary 获取用户病史
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /history/getUserDiseaseHistory [get]
func (m *MedicalHistoryApi) GetUserMedicalHistory(c *gin.Context) {
	id := utils.GetUserID(c)
	if result, err := MedicalHistoryService.GetUserMedicalHistory(id); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.Result(200, result, "获取成功", c)
	}
}
